﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace TowerClimber
{
    class Player
    {
        #region PlayerVariables
        public bool onGround, jumpingUp, touchingPlat = false;
        public bool alive = true;
        public bool facingLeft;
        public bool loop = true;
        public float moveSpeed = 3f;
        public float horizontalSpeed, verticalSpeed = 0f;
        public Vector2 position;
        public Texture2D playerTexture;
        public Rectangle playerBoundaries;
        public Animation currentPlayerAnimation;
        #endregion

        public void initializePlayer()
        {
            playerBoundaries = new Rectangle((int)position.X, (int)position.Y, 48, 48);
            currentPlayerAnimation = new Animation();
        }

        public void updatePlayer(ContentManager content, GameTime gameTime)
        {
            position.X += horizontalSpeed;
            position.Y += verticalSpeed;
            playerBoundaries.X = (int)position.X;
            playerBoundaries.Y = (int)position.Y;
            currentPlayerAnimation.Position.X = position.X;
            currentPlayerAnimation.Position.Y = position.Y;
            

            #region AnimationType

            if (jumpingUp == true)
            {
                if ((facingLeft == true) && (playerTexture != content.Load<Texture2D>("PlayerTextures/jumpingLeft")))
                {
                    playerTexture = content.Load<Texture2D>("PlayerTextures/jumpingLeft");
                    currentPlayerAnimation.Initialize(playerTexture, position, playerTexture.Width, playerTexture.Height, 1, 50, Color.White, 1f, true);
                }
                if ((facingLeft == false) && (playerTexture != content.Load<Texture2D>("PlayerTextures/jumpingRight")))
                {
                    playerTexture = content.Load<Texture2D>("PlayerTextures/jumpingRight");
                    currentPlayerAnimation.Initialize(playerTexture, position, playerTexture.Width, playerTexture.Height, 1, 50, Color.White, 1f, true);
                }
            }
            if (jumpingUp == false)
            {
                if (onGround == true)
                {
                    if ((horizontalSpeed > 0) && (playerTexture != content.Load<Texture2D>("PlayerTextures/walkingRight")))
                    {
                        playerTexture = content.Load<Texture2D>("PlayerTextures/walkingRight");
                        currentPlayerAnimation.Initialize(playerTexture, position, playerTexture.Width / 4, playerTexture.Height, 4, 50, Color.White, 1f, true);

                    }
                    if ((horizontalSpeed < 0) && (playerTexture != content.Load<Texture2D>("PlayerTextures/walkingLeft")))
                    {
                        playerTexture = content.Load<Texture2D>("PlayerTextures/walkingLeft");
                        currentPlayerAnimation.Initialize(playerTexture, position, playerTexture.Width / 4, playerTexture.Height, 4, 50, Color.White, 1f, true);
                    }
                }
                if ((horizontalSpeed == 0) || (onGround == false))
                {
                    if ((facingLeft == true) && (playerTexture != content.Load<Texture2D>("PlayerTextures/standingLeft")))
                    {
                        playerTexture = content.Load<Texture2D>("PlayerTextures/standingLeft");
                        currentPlayerAnimation.Initialize(playerTexture, position, playerTexture.Width, playerTexture.Height, 1, 5, Color.White, 1f, true);
                    }
                    if ((facingLeft == false) && (playerTexture != content.Load<Texture2D>("PlayerTextures/standingRight")))
                    {
                        playerTexture = content.Load<Texture2D>("PlayerTextures/standingRight");
                        currentPlayerAnimation.Initialize(playerTexture, position, playerTexture.Width, playerTexture.Height, 1, 5, Color.White, 1f, true);
                    }
                }
            }

            #endregion

            currentPlayerAnimation.Update(gameTime);
        }

        public void drawPlayer(SpriteBatch spriteBatch)
        {

            currentPlayerAnimation.Draw(spriteBatch);

        }
    }
}
